Queueλ FIFO μ리μ λ°λ₯Έ μ λ ¬λ 컬λ μ μ΄λ€.
function Queue() {
let items = [];
this.enqueue = function (element) {
// O(1)
items.push(element);
};
this.dequeue = function () {
// O(n)
return items.shift();
};
this.front = function () {
// O(1)
return items[0];
};
this.isEmpty = function () {
return !items.length;
};
this.clear = function () {
items = [];
};
this.size = function () {
return items.length;
};
this.print = function () {
console.log(items.toString());
};
}
Arrayλ₯Ό μ¬μ©ν΄ ꡬννλ©΄ μκΈ°λ λ¨μ μ dequeue
μμ λ§€λ² O(n)
λ²μ μ°μ°μ΄ λ°μνλ€λ κ²μ΄λ€. queue
μ μ¬μ΄μ¦κ° ν΄ κ²½μ° μ€λ²ν€λκ° λ§μ΄ λ°μν κ°λ₯μ±μ΄ ν¬κΈ° λλ¬Έμ λ€μκ³Ό κ°μ΄ ꡬννμ.
function Queue() {
function Node(value) {
this.value = value;
this.next = null;
}
let front = null;
let rear = null;
let length = 0;
this.enqueue = function (value) {
// μλ‘μ΄ λ
Έλ μμ±
const newNode = new Node(value);
if (!rear) {
// μ²μ enqueueνλ κ²½μ°
front = rear = newNode;
} else {
// κ°μ₯ λ λ
Έλμ μλ‘μ΄ λ
Έλ μΆκ°
rear.next = newNode;
// λ ν¬μΈν° λ³κ²½
rear = newNode;
}
length += 1;
};
this.dequeue = function () {
if (!front) return null;
// κ°μ₯ 첫 λ²μ§Έ λ
Έλ κ°μ Έμ΄
const dequeuedNode = front;
// λ λ²μ§Έ λ
Έλλ₯Ό μμ ν¬μΈν°λ‘ μ€μ
front = front.next;
if (!front) {
// λ§μ§λ§ λ
Έλμμ κ²½μ°
rear = null;
}
length -= 1;
return dequeuedNode.value;
};
this.front = function () {
return front;
};
// ...μ΄ν μλ΅
}
μμ κ°μ΄ Linked Listλ‘ κ΅¬ννκ² λλ©΄ enqueue
, dequeue
λͺ¨λ O(1)
λ§μ μμλ₯Ό μΆκ°, μ κ±°κ° κ°λ₯νλ€.
μ°μ μμ νλ κ° μμμ μ°μ μμλ₯Ό λΆμ¬ν΄ μ€λ¦μ°¨μ, λ΄λ¦Όμ°¨μμΌλ‘ μ λ ¬λ 컬λ μ μΌλ‘ μ¬μ©νλ μλ£κ΅¬μ‘°μ΄λ€.
function PriorityQueue() {
function QueueElement(element, priority) {
this.element = element;
this.priority = priority;
}
let items = [];
this.enqueue = function(element, priority) {
const element = new QueueElement(element, priority);
if (this.isEmpty()) {
items.push(element);
} else {
const added = false;
for (let i = 0; i < items.length; i++) {
if (element.priority < items[i].priority) {
items.splice(i, 0, element);
added = true;
break;
}
}
if (!added) {
items.push(element);
}
// o(N log N)
// items.sort();
}
// ...μ΄ν μλ΅
}
μ μ½λλ O(n)
μ μκ°μ΄ 걸리λ―λ‘, λ리λ€κ³ ν μ μλ€.
class MinHeap {
constructor() {
this.heap = [null];
}
push(value) {
this.heap.push(value);
let currentIndex = this.heap.length - 1;
let parentIndex = Math.floor(currentIndex / 2);
while (parentIndex !== 0 && this.heap[parentIndex] > value) {
const temp = this.heap[parentIndex];
this.heap[parentIndex] = value;
this.heap[currentIndex] = temp;
currentIndex = parentIndex;
parentIndex = Math.floor(currentIndex / 2);
}
}
pop() {
if (this.heap.length === 2) return this.heap.pop(); // λ£¨νΈ μ μ λ§ λ¨μ κ²½μ°
const returnValue = this.heap[1];
this.heap[1] = this.heap.pop();
let currentIndex = 1;
let leftIndex = 2;
let rightIndex = 3;
while (
this.heap[currentIndex] > this.heap[leftIndex] ||
this.heap[currentIndex] > this.heap[rightIndex]
) {
if (this.heap[leftIndex] > this.heap[rightIndex]) {
const temp = this.heap[currentIndex];
this.heap[currentIndex] = this.heap[rightIndex];
this.heap[rightIndex] = temp;
currentIndex = rightIndex;
} else {
const temp = this.heap[currentIndex];
this.heap[currentIndex] = this.heap[leftIndex];
this.heap[leftIndex] = temp;
currentIndex = leftIndex;
}
leftIndex = currentIndex * 2;
rightIndex = currentIndex * 2 + 1;
}
return returnValue;
}
}
heap
μ μ½μ
κ³Ό μμ λͺ¨λ O(log n)
μ μκ°μ΄ μμλλ―λ‘ μ λ ¬μ μ¬μ©νλ κ²λ³΄λ€ λΉ λ₯Έ μλλ₯Ό λΌ μ μλ€.
μ΄λ λ― μ°μ μμ νλ μ λ ¬μ λ°λΌ μ΅μ μ°μ μμ ν λλ μ΅λ μ°μ μμ νλ‘ λΆλ¦°λ€.
λ λ€λ₯Έ νμ λ³νμΈ νν νλ₯Ό μμ보μ.
μμ κ·Έλ¦¬κ³ μ μμ΄λ€μ΄ λ¨κ±°μ΄ κ°μλ₯Ό μ μ¬λμκ² μ΅λν 빨리 μ λ¬νλ€κ°, κ°μκΈ° λͺ¨λ λμμ λ©μΆκ³ κ·Έ λ λ¨κ±°μ΄ κ°μλ₯Ό μμ λ€κ³ μλ μμ΄λ₯Ό λ²μΉμΌλ‘ ν΄μ₯μν€λ κ²μμ΄λ€.
// ...Queue
// N -> nameList.length
// K -> num
// O(NK)
function hotPotato(nameList, num) {
// linked list
const queue = new Queue();
for (let i = 0; i < nameList.length; i++) {
queue.enqueue(nameList[i]);
}
let eliminated = "";
while (queue.size() > 1) {
for (let i = 0; i < num; i++) {
queue.enqueue(queue.dequeue());
}
eliminated = queue.dequeue();
console.log(eliminated + " (μ)λ₯Ό λ¨κ±°μ΄ κ°μ κ²μμμ ν΄μ₯μν΅λλ€.");
}
// O(N * K)
return queue.dequeue();
}
let players = ["Alice", "Bob", "Charlie", "David", "Eve"];
let numPasses = 7;
console.log("winner:", hotPotato(players, numPasses));
μ¬λ΄μΌλ‘ μκ³ λ¦¬μ¦μ νμ©νλ©΄ μλ£κ΅¬μ‘° μμ΄ λ λΉ λ₯Έ μκ°(O(n)
) μμ ν΄κ²° κ°λ₯νλ€
// μ νμ: f(n, k) = ((f(n - 1, k) + k) % n) + 1
// f(1, k) = 1 : κΈ°μ 쑰건
// f(n - 1, k) -> n - 1 : λ€μ μμ μμΉ
// k: λ€μ μμλ‘ μ΄λν΄μΌν νμ
// (f(n - 1, k) + k) % n :
// ex) 5 > 7
// 7 % 5 = 2
// 2 + 1 = 3
// O(N)
function findTheWinner(n, k) {
if (n === 1) return 1;
return ((findTheWinner(n - 1, k) + k) % n) + 1;
}
let players = ["Alice", "Bob", "Charlie", "David", "Eve"];
let numPasses = 7;
console.log("winner:", players[findTheWinner(players.length, numPasses) - 1]);
function findTheWinner(n, k) {
let s = 1;
for (let i = 1; i < n + 1; i++) {
s = ((s + k) % i) + 1;
}
return s;
}
let players = ["Alice", "Bob", "Charlie", "David", "Eve"];
let numPasses = 7;
console.log("winner:", players[findTheWinner(players.length, numPasses) - 1]);